package com.allinpay.merchantweb.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.allinpay.merchantweb.common.utils.Layui;
import com.allinpay.merchantweb.common.utils.MD5;
import com.allinpay.merchantweb.constants.CommonConstants;
import com.allinpay.merchantweb.constants.InputCode;
import com.allinpay.merchantweb.model.AipInsInfo;
import com.allinpay.merchantweb.model.CommManagerListRequest;
import com.allinpay.merchantweb.model.CommRoleInfo;
import com.allinpay.merchantweb.model.EnterpriseInfo;
import com.allinpay.merchantweb.model.Manage;
import com.allinpay.merchantweb.model.ManageAdd;
import com.allinpay.merchantweb.model.Manager;
import com.allinpay.merchantweb.model.UserLoginInfo;
import com.allinpay.merchantweb.response.ResponseData;
import com.allinpay.merchantweb.service.CommManagerListService;
import com.allinpay.merchantweb.service.ICommPrivilegeService;
import com.allinpay.merchantweb.service.IEnterpriseInfoService;
import com.allinpay.merchantweb.service.impl.RestRequestService;

@Controller
@RequestMapping("/setting")
public class ManagerController {
	
	private static final Logger logger = LoggerFactory.getLogger(ManagerController.class);
	
	@Autowired
	RestRequestService restRequestService;
	@Autowired
	CommManagerListService commManagerListService;
	@Autowired
	ICommPrivilegeService commPrivilegeService;
	@Autowired
	IEnterpriseInfoService enterpriseInfoService;
	
	/**
     * 功能：管理员查询初始化
     *
     */
    @RequestMapping(value = "/manage")
    public String manager(@ModelAttribute UserLoginInfo userLoginInfo, Model model, HttpServletRequest request) {

    	return "setting/manager/manage";
    }
    
	/**
     * 功能：管理员列表查询
     *
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object  list(@RequestParam Map<String, String> params,Manage manage,HttpServletRequest requests){
        
    	//从 session中获取数据
    	Manager lr=(Manager) requests.getSession().getAttribute(CommonConstants.SESSIONMANAGER);
    	//调用后台接口获取管理员数据
    	CommManagerListRequest request =new CommManagerListRequest();
    	//查询条件
    	request.setUserAcct(manage.getUserAcct());
    	//登录用户信息
        request.setUserAccount(lr.getUserAccount());
    	request.setUserType(lr.getUserType());
    	request.setOrgCode(lr.getOrgCode());
    	request.setPageNm(params.get("page"));
    	request.setPageSize(params.get("limit"));
		Layui retObject = null;
		try {
			retObject = commManagerListService.queryManagerList2(request);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e.toString());
		}
		return retObject;
    }
    
    
	/**
     * 功能：管理员查询
     *
     */
    @RequestMapping(value = "/addManager")
    public String addManager(@ModelAttribute Manage manage, Model model, HttpServletRequest request) {
    	Manager lr=(Manager) request.getSession().getAttribute(CommonConstants.SESSIONMANAGER);
    	CommRoleInfo requests =new CommRoleInfo();
    	requests.setPageNum(1);
    	requests.setNumPerPage(100);
    	requests.setInstanceId(lr.getInstanceId());
    	model.addAttribute("commRoleInfo", requests);  
    	EnterpriseInfo enterpriseInfo=new EnterpriseInfo();
    	enterpriseInfo.setInstanceId(lr.getInstanceId());
    	enterpriseInfo.setAipIns(lr.getAipIns());
		try {
			if(lr.getUserType()==3) {
				List<AipInsInfo> list=InputCode.getDesc();
				model.addAttribute("apinsList",list);
			}else {
				ResponseData<EnterpriseInfo> resp=enterpriseInfoService.queryEnterpriseInfo(enterpriseInfo);
				model.addAttribute("apinsList", resp.getDataList());
			}
		ResponseData<CommRoleInfo> respBean =	commPrivilegeService.queryRoleInfoList(requests);
		model.addAttribute("roleList", respBean.getDataList());  
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(lr.getUserType()==3 && lr.getOrgCode().equals(CommonConstants.SUPER_AIPINS)) {  //通联总部
    	    return "setting/manager/manageAdd";
		}else if(lr.getUserType()==3 && !lr.getOrgCode().equals(CommonConstants.SUPER_AIPINS)){ //通联分公司
			return "setting/manager/manageAddQy";	
		}else {       //企业
			return "setting/manager/manageAddQys";
		}
    }
    
    @RequestMapping("/addSave")
    @ResponseBody
    public Object  addSave(@ModelAttribute ManageAdd manage,HttpServletRequest request){
        //查询列表数据
		Layui retObject = null;
		manage.setPassWord(MD5.MD5Encode(manage.getPassWord()));
		try {
			Manager lr=(Manager) request.getSession().getAttribute(CommonConstants.SESSIONMANAGER);
			manage.setOrgCode(lr.getOrgCode());
			if(lr.getUserType()!=3) {
				manage.setInstanceId(lr.getInstanceId());
				manage.setAipIns(lr.getAipIns());
			}
			if(lr.getUserType()==3) {
			retObject = commManagerListService.addAllManager(manage);
			}else {
				retObject = commManagerListService.addManager(manage);	
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return retObject;
    }
    
    /**
     * 功能：管理员查询
     *
     */
    @RequestMapping(value = "/modifyManager")
    public String modifyManager(@ModelAttribute Manage manage, Model model, HttpServletRequest request) {
    	Manager lr=(Manager) request.getSession().getAttribute(CommonConstants.SESSIONMANAGER);
    	CommManagerListRequest requests =new CommManagerListRequest();
    	requests.setUserAccount(lr.getUserAccount());
    	requests.setUserType(lr.getUserType());
    	requests.setOrgCode(lr.getOrgCode());
    	requests.setInstanceId(lr.getInstanceId());

    	requests.setPageNm("1");
    	requests.setPageSize("10");
		Manage retObject = null;
    	CommRoleInfo requests1 =new CommRoleInfo();
    	requests1.setPageNum(1);
    	requests1.setNumPerPage(100);
    	requests1.setInstanceId(lr.getInstanceId());
    	EnterpriseInfo enterpriseInfo=new EnterpriseInfo();
    	enterpriseInfo.setInstanceId(lr.getInstanceId());
    	enterpriseInfo.setAipIns(lr.getAipIns());
		try {
			if(lr.getUserType()==3) {
				List<AipInsInfo> list=InputCode.getDesc();
				model.addAttribute("apinsList",list);
			}else {
				ResponseData<EnterpriseInfo> resp=enterpriseInfoService.queryEnterpriseInfo(enterpriseInfo);
				model.addAttribute("apinsList", resp.getDataList());
			}
			retObject = commManagerListService.queryManagerList(requests);
			ResponseData<CommRoleInfo> respBean =	commPrivilegeService.queryRoleInfoList(requests1);
			model.addAttribute("roleList", respBean.getDataList());  
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
/*		retObject.setPassWord(Md5.m);*/
		model.addAttribute("manage", retObject); 
		model.addAttribute("role", retObject.getRole()); 
		if(lr.getUserType()==3 && lr.getOrgCode().equals(CommonConstants.SUPER_AIPINS)) {
		     return "setting/manager/manageModify";
		}else if((lr.getUserType()==3 && !lr.getOrgCode().equals(CommonConstants.SUPER_AIPINS))){
			return "setting/manager/manageModifyQy";	
		}else {
			return "setting/manager/manageModifyQys";	
		}
    }
  
    @RequestMapping("/modifySave")
    @ResponseBody
    public Object  modifySave(@ModelAttribute ManageAdd manage,HttpServletRequest request){
    	Manager lr=(Manager) request.getSession().getAttribute(CommonConstants.SESSIONMANAGER);
        //查询列表数据
		manage.setOrgCode(lr.getOrgCode());;
		Layui retObject = null;
		try {
			if(lr.getUserType()!=3) {
				manage.setInstanceId(lr.getInstanceId());	
			}
			retObject = commManagerListService.updateManager(manage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return retObject;
    }
    
    @RequestMapping("/delete")
    @ResponseBody
    public Object  delete(@ModelAttribute Manage manage,HttpServletRequest requests){
        //查询列表数据
    	Manage request =new Manage();
    	Manager lr=(Manager) requests.getSession().getAttribute(CommonConstants.SESSIONMANAGER);
    	request.setUserAccount(manage.getUserAccount());
    	request.setOrgCode(lr.getOrgCode());
		Layui retObject = null;
		try {
			retObject = commManagerListService.deleteManager(request);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return retObject;
    }
    
    
	/**
     * 功能：管理员查询
     *
     */
    @RequestMapping(value = "/changePassWord")
    public String changePassWord(@ModelAttribute Manage manage, Model model, HttpServletRequest request) {
		model.addAttribute("manage", manage);  
    	return "setting/manager/changePassword";
    }
    
    
    @RequestMapping("/changePassWordSave")
    @ResponseBody
    public Object  changePassWordSave(@ModelAttribute Manage manage,HttpServletRequest request){
        //查询列表数据
		Layui retObject = null;
		manage.setPassWord(MD5.MD5Encode(manage.getPassWord()));
		try {
			Manager lr=(Manager) request.getSession().getAttribute(CommonConstants.SESSIONMANAGER);
			manage.setOrgCode(lr.getOrgCode());
			retObject = commManagerListService.changePassWord(manage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return retObject;
    }
}
